package com.micro.glszh.stat.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.micro.glszh.stat.utils.LocalUserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author gaopengfei
 * @version 1.0
 * @description: mybatis 自动插入创建人创建时间等信息
 * @date 2023/8/23 10:50
 */
@Slf4j
@Component
public class MetaMybatisPlusHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        // 或者 // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "createBy", LocalUserUtils.getUser()::getName, String.class);
        this.strictInsertFill(metaObject, "createTime", LocalDate::now, LocalDate.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        // 或者 // 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "updateBy", LocalUserUtils.getUser()::getName, String.class);
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
    }
}
